#include "precompiled.h"
#include "filteritemsdialog.h"
using namespace std;
using namespace weo::engine::item;
using namespace weo::ui;
FilterItemsDialog::FilterItemsDialog(QWidget* parent /*= 0*/, Qt::WFlags flags /*= 0*/) : QDialog(parent, flags)
{
    setupUi(this);
}
void FilterItemsDialog::setValues()
{
    FilterILevelBelowLineEdit->setText(tr(boost::lexical_cast<std::string>(m_Character->getFilterBelow()).c_str()));
    FilterILevelAboveLineEdit->setText(tr(boost::lexical_cast<std::string>(m_Character->getFilterAbove()).c_str()));
    std::map<std::string,bool>::const_iterator it;
    for(it = m_Character->getFilterArea()->begin();it!= m_Character->getFilterArea()->end();it++)
    {
        FilterAreaListWidget->addItem(tr(it->first.c_str()));
    }
    for(it = m_Character->getFilterArea()->begin();it!= m_Character->getFilterArea()->end();it++)
    {
        QList<QListWidgetItem *>items = FilterAreaListWidget->findItems(tr(it->first.c_str()),Qt::MatchFixedString);
        if(items.empty()) continue;
        QListWidgetItem *item = items[0];
        item->setSelected(it->second);
    }
    std::vector<std::string> list = Item::GetItemSources();
    unsigned int source = m_Character->getFilterSource();
    std::vector<std::string>::const_iterator it2;
    for(it2 = list.begin();it2 != list.end();it2++)
    {
        FilterSourceListWidget->addItem(tr(it2->c_str()));
    }
    for(it2 = list.begin();it2 != list.end();it2++)
    {
        QList<QListWidgetItem *>items = FilterSourceListWidget->findItems(tr(it2->c_str()),Qt::MatchFixedString);
        if(items.empty()) continue;
        QListWidgetItem *item = items[0];
        if(source & Item::GetItemsource(it2->c_str()))
        {
            item->setSelected(true);
        }
    }
    std::vector<std::string> profList = Item::GetProfessions();
    unsigned int profession = m_Character->getFilterProfession();
    for(it2 = profList.begin();it2 != profList.end();it2++)
    {
        FilterProfessionListWidget->addItem(tr(it2->c_str()));
    }
    for(it2 = profList.begin();it2 != profList.end();it2++)
    {
        QList<QListWidgetItem *>items = FilterProfessionListWidget->findItems(tr(it2->c_str()),Qt::MatchFixedString);
        if(items.empty()) continue;
        QListWidgetItem *item = items[0];
        if(profession & Item::GetProfession(it2->c_str()))
        {
            item->setSelected(true);
        }
    }
    std::vector<std::string> qualList = Item::GetQualities();
    unsigned int quality = m_Character->getFilterQuality();
    for(it2 = qualList.begin();it2 != qualList.end();it2++)
    {
        FilterQualityListWidget->addItem(tr(it2->c_str()));
    }
    for(it2 = qualList.begin();it2 != qualList.end();it2++)
    {
        QList<QListWidgetItem *>items = FilterQualityListWidget->findItems(tr(it2->c_str()),Qt::MatchFixedString);
        if(items.empty()) continue;
        QListWidgetItem *item = items[0];
        if(quality & Item::GetQuality(it2->c_str()))
        {
            item->setSelected(true);
        }
    }
}
void FilterItemsDialog::getValues()
{
    m_Character->setFilterBelow(boost::lexical_cast<unsigned int>(FilterILevelBelowLineEdit->text().toStdString()));
    m_Character->setFilterAbove(boost::lexical_cast<unsigned int>(FilterILevelAboveLineEdit->text().toStdString()));
    QList<QListWidgetItem *>items = FilterAreaListWidget->selectedItems();
    for(int i = 0;i<items.size();i++)
    {
        m_Character->toggleFilterArea(items[i]->text().toStdString(),true);
    }
    items = FilterSourceListWidget->selectedItems();
    unsigned int sourceValue = 0;
    for(int i = 0;i<items.size();i++)
    {
        sourceValue |= Item::GetItemsource(items[i]->text().toStdString());
    }
    m_Character->setFilterSource(sourceValue);
    items = FilterProfessionListWidget->selectedItems();
    unsigned int profValue = 0;
    for(int i = 0;i<items.size();i++)
    {
        profValue |= Item::GetProfession(items[i]->text().toStdString());
    }
    m_Character->setFilterProfession(profValue);
    items = FilterQualityListWidget->selectedItems();
    unsigned int qualValue = 0;
    for(int i = 0;i<items.size();i++)
    {
        qualValue |= Item::GetQuality(items[i]->text().toStdString());
    }
    m_Character->setFilterQuality(qualValue);
}